﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;

namespace AbhiRuchiFoods
{
    /// <summary>
    /// Interaction logic for EmployeeTaskMapping.xaml
    /// </summary>
    public partial class EmployeeTaskMapping : UserControl
    {
        string id = "0";
        public EmployeeTaskMapping()
        {
            InitializeComponent();
            bindGrid();
            LoadSections();
            LoadEmpType();
        }

        private void LoadEmpType()
        {
            DataTable dtEmpType = new DataTable();
            dtEmpType.Columns.Add("EmployeeType", typeof(System.String));
            dtEmpType.Columns.Add("EmployeeTypeId", typeof(System.Int32));
            DataRow dr1=dtEmpType.NewRow();
            dr1["EmployeeType"]="Permanent";
            dr1["EmployeeTypeId"]="1";

            DataRow dr2=dtEmpType.NewRow();
            dr2["EmployeeType"]="Temporary";
            dr2["EmployeeTypeId"]="0";

            dtEmpType.Rows.Add(dr1);
            dtEmpType.Rows.Add(dr2);

            cmbEmbType.ItemsSource = dtEmpType.DefaultView;
            cmbEmbType.DisplayMemberPath = dtEmpType.Columns["EmployeeType"].ToString();
            cmbEmbType.SelectedValuePath = dtEmpType.Columns["EmployeeTypeId"].ToString(); ;
        }

        private void LoadSections()
        {
            DataTable dt = new DataTable();
            dt = AbhiruchiDAL.SectionList();
            cmbSections.DataContext = dt.DefaultView;
            cmbSections.DisplayMemberPath = dt.Columns["SectionName"].ToString();
            cmbSections.SelectedValuePath = dt.Columns["SectionId"].ToString();
        }

        private void bindGrid()
        {
            DataTable dtTasks = new DataTable();
            dtTasks = AbhiruchiDAL.GetEmployeeTaskMapping();
            gvEmpTaskMapping.ItemsSource = dtTasks;
        }

        private void LoadEmployee(int empType)
        {
            DataTable dt = new DataTable();
            dt = AbhiruchiDAL.getEmployeeListBasedOnEmployeeType(empType);
            cmbEmpName.DataContext = dt.DefaultView;
            cmbEmpName.DisplayMemberPath = dt.Columns["EmployeeName"].ToString();
            cmbEmpName.SelectedValuePath = dt.Columns["Id"].ToString();
        }

        private bool DoValidations()
        {
            if (cmbEmbType.SelectedIndex==-1)
            {
                MessageBox.Show("Please Select Employee Type", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
            if (cmbEmpName.SelectedIndex == -1)
            {
                MessageBox.Show("Please Select Employee Name", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
            if (cmbSections.SelectedIndex == -1)
            {
                MessageBox.Show("Please Select Section Name", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
            if (cmbTask.SelectedIndex == -1)
            {
                MessageBox.Show("Please Select One Task", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
            if (dtStart.Text == "")
            {
                MessageBox.Show("Please Select Start Date", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
            if (dtStart.Text == "")
            {
                MessageBox.Show("Please Select End Date", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
            return true;
        }

        private void clearAllFields()
        {
            cmbEmbType.SelectedIndex = -1;
            cmbEmpName.SelectedIndex = -1;
            cmbSections.SelectedIndex = -1;
            cmbTask.SelectedIndex = -1;
            dtStart.Text = "";
            dtEnd.Text = "";
            txtRemarks.Text = "";
        }

        private void LoadTask(int sectionType)
        {
            DataTable dt = new DataTable();
            dt = AbhiruchiDAL.getTaskListBasedOnSection(sectionType);
            cmbTask.DataContext = dt.DefaultView;
            cmbTask.DisplayMemberPath = dt.Columns["TaskName"].ToString();
            cmbTask.SelectedValuePath = dt.Columns["TaskId"].ToString();
        }

        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            if (DoValidations() == true)
            {
                if (id == "0")
                {
                    DataTable dtCheckEmp = new DataTable();
                    dtCheckEmp = AbhiruchiDAL.GetCountEmployeeTaskMapping(Convert.ToInt32(cmbEmpName.SelectedValue));
                    if (dtCheckEmp.Rows[0][0].ToString() == "0")
                    {
                        AbhiruchiDAL.setEmployeeTaskMapping(cmbTask.SelectedValue.ToString(), cmbEmpName.SelectedValue.ToString(), dtStart.Text.ToString(), dtEnd.Text.ToString(), txtRemarks.Text);
                        clearAllFields();
                        MessageBox.Show("Employee Task mapping Done Successfully", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                    else
                    {
                        MessageBox.Show("This Employee Is Already Mapped To Another Task", "Information", MessageBoxButton.OK, MessageBoxImage.Information);

                    }
                }
                else
                {
                    AbhiruchiDAL.updateEmployeeTaskMapping(id,cmbTask.SelectedValue.ToString(),cmbEmpName.SelectedValue.ToString(),dtStart.Text.ToString(),dtEnd.Text.ToString(),txtRemarks.Text);
                    clearAllFields();
                    MessageBox.Show("Employee Task mapping Modified Successfully", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                    id = "0";
                }
                bindGrid();
            }

        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            clearAllFields();
        }

        private void cmbEmbType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbEmbType.SelectedIndex != -1)
            {
                int i = Convert.ToInt32(cmbEmbType.SelectedValue);
                LoadEmployee(i);
            }
        }

        private void cmbSections_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbSections.SelectedIndex != -1)
            {
                int i = Convert.ToInt32(cmbSections.SelectedValue);
                LoadTask(i);
            }
        }

        private void dtStart_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dtStart.SelectedDate != null)
            {
                DateTime dt = Convert.ToDateTime(dtStart.Text.ToString());
                dt = dt.AddMonths(1);
                dtEnd.Text = dt.ToString();
            }
        }

        private void gvEmpTaskMapping_RowActivated(object sender, Telerik.Windows.Controls.GridView.RowEventArgs e)
        {
            id = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[0])).Value.ToString();

            string  taskName = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[1])).Value.ToString();
            LoadSections();
            DataTable dtTask = new DataTable();
            dtTask = AbhiruchiDAL.getSectionBasedOnTaskId(taskName);
            cmbSections.SelectedValue = dtTask.Rows[0][0].ToString();
            LoadTask(Convert.ToInt32(dtTask.Rows[0][0]));
            cmbTask.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[1])).Value.ToString();

            string empName = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[2])).Value.ToString();
            DataTable dtEmp = new DataTable();
            dtEmp = AbhiruchiDAL.getEmployeeTypeBasedOnId(empName);
            cmbEmbType.SelectedValue = dtEmp.Rows[0][0].ToString();
            LoadEmployee(Convert.ToInt32(dtEmp.Rows[0][0]));
            cmbEmpName.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[2])).Value.ToString();
            
            dtStart.Text = DateTime.Now.AddDays(1).ToString();
            DateTime dt = DateTime.Now.AddMonths(1).AddDays(1);
            dtEnd.Text = dt.ToString();

            txtRemarks.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[5])).Value.ToString();
        }    
    }
}
